/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.managers;

import java.util.Properties;
import javax.swing.InputVerifier;
import javax.swing.text.JTextComponent;
import org.balisunrise.components.ComponentProperties;
import org.balisunrise.common.Converter;
import org.balisunrise.components.conveters.StringConverter;
import org.balisunrise.swing.components.ComponentFactory;

/**
 *
 * @author glacuo
 */
public class StringComponentManager
        extends JTextFieldComponentManager<String, JTextComponent>{

    private static final StringConverter CONVERTER = new StringConverter(false);
    private static final StringConverter CONVERTER_NULL = new StringConverter(true);

    @Override
    public Class<String> getType() {
        return String.class;
    }

    @Override
    public Class<JTextComponent> getComponentType() {
        return JTextComponent.class;
    }

    @Override
    public Converter<String, String> getConverter(boolean nullForEmpty) {
        if(nullForEmpty)
            return CONVERTER_NULL;
        else
            return CONVERTER;
    }

    @Override
    public JTextComponent buildComponent(Properties p) {

        boolean upper = true;
        int length = 20;
        int lines = 0;
        String mask = null;
        InputVerifier inputVerifier = null;

        length = ComponentProperties
                .getInt(p, ComponentProperties.LENGTH, length);
        upper = ComponentProperties
                .getBoolean(p, ComponentProperties.UPPER, upper);
        lines = ComponentProperties
                .getInt(p, ComponentProperties.LINES, lines);
        mask = ComponentProperties
                .getString(p, ComponentProperties.MASK, mask);

        Object o = p.get(ComponentProperties.INPUT_VERIFIER);
        if(o != null && o instanceof InputVerifier)
            inputVerifier = (InputVerifier) o;

        // se possuir mascara, cria componente para mascara.
        if(mask != null)
            return ComponentFactory.maskTextField(mask, inputVerifier);

        // se possuir mais de 60 chars, cria componente de várias linhas.
        if(length > 60){
            return ComponentFactory.multLineField(length, lines, upper);
        }

        // por fim cria componente de texto de uma linha.
        return ComponentFactory.textField(length, upper);
    }
}
